<template>
  <div>
    <el-form ref="addFormRef" :rules="rules" :model="addForm.data" label-width="120">
      <el-form-item label="运营商" prop="operator">
        <el-select
          v-model="addForm.data.operator"
          filterable
          style="width: 320px"
          placeholder="请选择运营商"
        >
          <el-option
            v-for="item in operator.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="账号" prop="account">
        <el-input
          v-model="addForm.data.account"
          style="width: 320px"
          placeholder="请输入账号"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="addForm.data.password"
          style="width: 320px"
          maxlength="20"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item label="开通时间" prop="openIn">
        <el-date-picker
          v-model="addForm.data.openIn"
          style="width: 320px"
          type="date"
          placeholder="请选择开通时间"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="到期时间" prop="expiresIn">
        <el-date-picker
          v-model="addForm.data.expiresIn"
          style="width: 320px"
          type="date"
          placeholder="请选择到期时间"
          value-format="YYYY-MM-DD"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="addForm.data.remark"
          style="width: 320px"
          placeholder="请输入备注"
        />
      </el-form-item>
      <el-form-item>
        <div class="footer">
          <div></div>
          <div>
            <el-button class="reset-btn" @click="resetForm(addFormRef)">重 置</el-button>
            <el-button class="confirm-btn" @click="submitForm(addFormRef)"
              >确 认</el-button
            >
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
const emits = defineEmits(["operationDone"]);
import { getAddBroadBand } from "../request.js";
import { getDictionary } from "@/service/modules/publicModule";

const addFormRef = ref("");
const addForm = reactive({
  data: {
    operator: "",
    account: "",
    password: "",
    openIn: "",
    expiresIn: "",
    remark: "",
  },
});

const operator = reactive({
  data: [],
});

const getData = async () => {
  await getDictionary("network_operator").then((res) => {
    operator.data = res.data.result;
  });
};

getData();

const rules = reactive({
  operator: [{ required: true, message: "请选择运营商", trigger: "change" }],
  account: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  openIn: [{ required: true, message: "请选择开通时间", trigger: "change" }],
  expiresIn: [{ required: true, message: "请选择到期时间", trigger: "change" }],
});

const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      getAddBroadBand(addForm.data).then((res) => {
        if (res.data.code === 0) {
          ElMessage({
            type: "success",
            message: res.data.result,
          });
          emits("operationDone");
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  addForm.data = {};
  formEl.resetFields();
};
</script>

<style lang="less" scoped>
.footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.reset-btn {
  width: 65px;
  height: 32px;
  background: #ffffff;
  border-radius: 2px;
  border: 1px solid #d9d9d9;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.65);
}
.confirm-btn {
  width: 65px;
  height: 32px;
  background: #007aff;
  border-radius: 2px;
  font-weight: 400;
  font-size: 14px;
  color: #ffffff;
}
</style>
